package com.educatioin.manage.web.schoolgradeclass.controller;

import com.educatioin.commons.util.AjaxResult;
import com.educatioin.manage.web.schoolgradeclass.po.SchoolGradeClassPo;
import com.educatioin.manage.web.schoolgradeclass.request.CreateSchoolGradeClassReq;
import com.educatioin.manage.web.schoolgradeclass.request.ListSchoolGradeClassReq;
import com.educatioin.manage.web.schoolgradeclass.request.ModifySchoolGradeClassReq;
import com.educatioin.manage.web.schoolgradeclass.response.ListSchoolGradeClassRsp;
import com.educatioin.manage.web.schoolgradeclass.service.SchoolGradeClassService;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by caizh on 18-5-5.
 */
@RestController
@RequestMapping(value = {"/api/schoolGradeClass"})
public class SchoolGradeClassController {

    @Resource
    private SchoolGradeClassService schoolGradeClassService;

    @RequestMapping("/list")
    @ResponseBody
    public ListSchoolGradeClassRsp list(ListSchoolGradeClassReq req) {
        return schoolGradeClassService.list(req);
    }

    @RequestMapping("/modify")
    @ResponseBody
    public AjaxResult modify(@RequestBody ModifySchoolGradeClassReq req) {
        AjaxResult result = new AjaxResult<>();
        schoolGradeClassService.modify(req);
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("modify success");
        return result;
    }

    @RequestMapping("/create")
    @ResponseBody
    public AjaxResult create(@RequestBody CreateSchoolGradeClassReq req) {
        AjaxResult result = new AjaxResult<>();
        schoolGradeClassService.create(req);
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("modify success");
        return result;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public AjaxResult findById(Long id) {
        AjaxResult result = new AjaxResult<>();
        result.setData(schoolGradeClassService.findById(id));
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("success");
        return result;
    }

    @RequestMapping("/listSelectOptions")
    @ResponseBody
    public AjaxResult listSelectOptions() {
        AjaxResult result = new AjaxResult<>();
        List<Map<String, Object>> schoolGradeClassList = Lists.newArrayList();
        Iterable<SchoolGradeClassPo> list = schoolGradeClassService.getSchoolGradeClassRepository().findAll();
        if (list != null) {
            list.forEach(s -> {
                Map<String, Object> sgc = Maps.newHashMap();
                sgc.put("id", s.getId());
                sgc.put("name", Joiner.on("/").join(s.getSchoolName(), s.getGrade(), s.getClazz()));
                schoolGradeClassList.add(sgc);
            });
        }
        result.setData(schoolGradeClassList);
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("success");
        return result;
    }
}
